package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.IWxMaterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WxInfo;
import com.ruoyi.system.service.IWxInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * infoController
 * 
 * @author ruoyi
 * @date 2021-07-31
 */
@Controller
@RequestMapping("/system/info")
public class WxInfoController extends BaseController
{
    private String prefix = "system/info";

    @Autowired
    private IWxInfoService wxInfoService;

    @Autowired
    private IWxMaterService wxMaterService;

    @RequiresPermissions("system:info:view")
    @GetMapping()
    public String info()
    {
        return prefix + "/info";
    }

    /**
     * 查询info列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxInfo wxInfo)
    {

        SysUser currentUser = ShiroUtils.getSysUser();
        List<SysDept> sysDepts;
        if(currentUser.getLoginName().equals("admin") || currentUser.getDeptId() == 101){
            sysDepts= wxMaterService.selectDeptsByMaterPhone("");
        }else{
            // 根据用户手机号，获取项目权限
            sysDepts = wxMaterService.selectDeptsByMaterPhone( ShiroUtils.getSysUser().getPhonenumber());
        }
        // 根据用户手机号，获取项目权限
        if(StringUtils.isNotEmpty(sysDepts)){
            List<Long> deptList = new ArrayList<>();
            for (SysDept dept : sysDepts){
                deptList.add(dept.getDeptId());
            }
            wxInfo.setDepts(deptList);
        }
        Map<Object, Object> resMap = new HashMap<>();
        resMap.put("name", wxInfo.getName());
        resMap.put("phone", wxInfo.getPhone());
        resMap.put("status", wxInfo.getStatus());
        resMap.put("depts",wxInfo.getDepts());

        startPage();
        List<WxInfo> list = wxInfoService.selectWxInfoLists(resMap);
        return getDataTable(list);
    }

    /**
     * 导出info列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "info", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WxInfo wxInfo)
    {
        List<WxInfo> list = wxInfoService.selectWxInfoList(wxInfo);
        ExcelUtil<WxInfo> util = new ExcelUtil<WxInfo>(WxInfo.class);
        return util.exportExcel(list, "info数据");
    }

    /**
     * 新增info
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存info
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "info", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxInfo wxInfo)
    {
        return toAjax(wxInfoService.insertWxInfo(wxInfo));
    }

    /**
     * 修改info
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WxInfo wxInfo = wxInfoService.selectWxInfoById(id);
        mmap.put("wxInfo", wxInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存info
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "info", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WxInfo wxInfo)
    {
        return toAjax(wxInfoService.updateWxInfo(wxInfo));
    }

    /**
     * 删除info
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "info", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wxInfoService.deleteWxInfoByIds(ids));
    }
}
